// pages/personal/addaddress/addaddress.js
var utils = require('../../../utils/util.js');
var app=getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        region: [],
        curHdIndex:"",
        id:"",
        isvalue:false,
        ischecked:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that=this;
        if (options.id!= undefined){
            if (options.tags == "家庭地址") {
                that.data.curHdIndex = 0
            }
            if (options.tags == "公司地址") {
                that.data.curHdIndex = 1
            }

            that.setData({
                id: options.id,
                name: options.name,
                region: options.address.split("-"),
                detail: options.detail,
                mobile: options.mobile,
                ischecked: options.isdefault,
                curHdIndex: that.data.curHdIndex,
                tags: options.tags
            })
            if (that.data.region.length > 0) {
                that.setData({
                    isPicker: true
                })
            }
        }

    },
    //地址类型
    tab:function(e){
        var that=this;
        var id = e.currentTarget.dataset.id;
        that.setData({
            curHdIndex:id,
            tags: e.currentTarget.dataset.name
        })
    },
    //设置默认地址
    switch1Change(e) {
        var that=this;
        if (e.detail.value==false){
            that.data.ischecked=0;
        }else{
            that.data.ischecked=1;
        }
        that.setData({
            ischecked: that.data.ischecked
        })
    },
    //选择地址
    bindRegionChange(e) {
        var that=this;
        this.setData({
            region: e.detail.value
        })
        if(that.data.region!=[]){
            that.setData({
                isPicker: true
            })
        }
    },
    formSubmit:function(e){
        var that=this;
        var userName=e.detail.value.userName;
        var userPhone=e.detail.value.userPhone;
        var userTextarea=e.detail.value.userTextarea;
        var address=that.data.region.join("-");  //地址
        if (userName != "" && (/^1(3|4|5|7|8)\d{9}$/.test(userPhone)) && address != "" && userTextarea != "" && that.data.tags!= ""){
            utils.http("/api/v1/address/add_modify_address", {
                id: that.data.id,
                name: userName,
                mobile: userPhone,
                address: address,
                detail: userTextarea,
                is_default: that.data.ischecked,
                tags: that.data.tags
            }, function (res) {
                if(res.state==1){
                    wx.showToast({
                        title: '保存成功',
                        duration: 2000
                    }) 
                    if(that.data.ischecked==1){
                        var ppp={
                            id: that.data.id,
                            name: userName,
                            mobile: userPhone,
                            address: address,
                            detail: userTextarea,
                            is_default: that.data.ischecked,
                            tags: that.data.tags
                        }
                        wx.setStorageSync("defaultAddress", ppp)
                    }
                    wx.navigateBack({})
                }
            })
        }else{
            if (that.data.tags == "") {
                wx.showToast({
                    title: '请填写地址类型',
                    image: 'https://xinong.qidian2018.com/smimgs/error.png',
                    duration: 2000
                })
            }
            if (userTextarea == "") {
                wx.showToast({
                    title: '请填写详细地址',
                    image: 'https://xinong.qidian2018.com/smimgs/error.png',
                    duration: 2000
                })
            }
            if (address==""){
                wx.showToast({
                    title: '请填写配送地址',
                    image: 'https://xinong.qidian2018.com/smimgs/error.png',
                    duration: 2000
                })
            }
            if (!(/^1(3|4|5|7|8)\d{9}$/.test(userPhone))) {
                wx.showToast({
                    title: '联系电话错误',
                    image: 'https://xinong.qidian2018.com/smimgs/error.png',
                    duration: 2000
                })
            }
            if(userName==""){
                wx.showToast({
                    title: '请填写收货人姓名',
                    image: 'https://xinong.qidian2018.com/smimgs/error.png',
                    duration: 2000
                })
            }

        }
        
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})